package com.example.springboot11.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot11.entiy.User;
import com.example.springboot11.mapper.UserMapper;
import com.example.springboot11.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述
 *
 * @author: liangshuai
 * @date: 2022年05月02日 9:43
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public List<User> queryAllUser() {
        QueryWrapper queryWrapper = new QueryWrapper();

        List<User> userList = userMapper.selectList(queryWrapper);
        return userList;
    }

    @Override
    public User QueryOne(User user){


        User user1 = (User) redisTemplate.opsForHash().get("user","user:"+user.getId());

        if (user1 == null){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",user.getId());
            user = userMapper.selectOne(queryWrapper);
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.opsForHash().put("user" ,"user:"+user.getId(),user);
            user1 = (User) redisTemplate.opsForHash().get("user","user:"+user.getId());
        }
        return user1;
    }


}
